package Model;

import java.sql.ResultSet;
import java.sql.Statement;

public class Faq {

	   private DbConnection db = null;
	   private Statement st = null;
	   private ResultSet rs = null; 
	   
	   private int id_ad=-1;
	   private String question=null;
	   private String answer=null;
	   private String date_pub=null;
	   private int id_user_question=-1;
	   private int id_faq=-1;
	   
	   
	   
	   public Faq(){
		   db = new DbConnection();	   
		   st = db.getStatement();
	   }
	   
	   public Faq(int id){
		   db = new DbConnection();	   
		   st = db.getStatement();
		   
		   try{
				this.rs = this.st.executeQuery("Select * from faq "+
												" WHERE id_faq="+id+";");
				this.rs.next();
				this.id_ad = this.rs.getInt("id_ad");
				this.question = this.rs.getString("question");
				this.answer= this.rs.getString("answer");
				this.date_pub = this.rs.getString("date_pub");
				this.id_user_question = this.rs.getInt("id_user_question");
				this.id_faq = this.rs.getInt("id_faq");
				
				
			}catch (Exception e) {
				System.out.print(e.toString());
			}   
	   }
	   
	   public int getIdAdvertising(){
		   return this.id_ad;
	   }
	   
	   public String getQuestion(){
		   return this.question;
	   }
	   
	   public String getAnswer(){
		   return this.answer;
	   }
	   
	   public String getDatePub(){
		   return this.date_pub;
	   }
	   
	   public int getIdUserQuestion(){
		   return this.id_user_question;
	   }
	   
	   public int getIdFaq(){
		   return this.id_faq;
	   }
	   
	   public void setFaq(int id){
		   db = new DbConnection();	   
		   st = db.getStatement();
		   
		   try{
				this.rs = this.st.executeQuery("Select * from faq "+
												" WHERE id_faq="+id+";");
				this.rs.next();
				this.id_ad = this.rs.getInt("id_ad");
				this.question = this.rs.getString("question");
				this.answer= this.rs.getString("answer");
				this.date_pub = this.rs.getString("date_pub");
				this.id_user_question = this.rs.getInt("id_user_question");
				this.id_faq = this.rs.getInt("id_faq");
				
				
			}catch (Exception e) {
				System.out.print(e.toString());
			}   
	   }
	   
	   
	   //METODY BAZODANOWE
	   
	   
	   public int insertFaq(int id_ad,String question,String answer,String date_pub,int  id_user_question){
		   int result=-1;
		   try{
				this.rs = this.st.executeQuery("INSERT INTO faq( "+
			            " id_ad, question, answer, date_pub, id_user_question) "+
			            " VALUES ( "+id_ad+", '"+question+"', '"+answer+"', '"+date_pub+"', "+id_user_question+") returning id_faq;");
				
				this.rs.next();
				result = this.rs.getInt("id_faq");
				
			}catch (Exception e) {
				System.out.print(e.toString());
				return result;
			}
			return result;
		}
	   
	   public int deleteFaqById(int id){
		   
		   int result = -1;
			try{
				this.rs = this.st.executeQuery("DELETE FROM faq WHERE id_faq="+id+" returning id_faq");
				
				this.rs.next();
				result = this.rs.getInt("id_faq");
				
			}catch (Exception e) {
				System.out.print(e.toString());
				return result;
			}
			return result;
		   
	   }
	   
	   public int updateFaq(int id_ad,String question,String answer,String date_pub,int  id_user_question, int id_faq){
		   int result=-1;
		   
			String q = "UPDATE \"user\" "+
			   "SET  id_ad="+id_ad+", question='"+question+"', answer='"+answer+"'," +
		   		" date_pub='"+date_pub+"', id_user_question="+id_user_question+
		  " WHERE id_faq="+id_faq+" returning id_faq;";
		  System.out.print(q);
		   
		   try{
				this.rs = this.st.executeQuery("UPDATE faq "+
											   "SET  id_ad="+id_ad+", question='"+question+"', answer='"+answer+"'," +
											   		" date_pub='"+date_pub+"', id_user_question="+id_user_question+
											  " WHERE id_faq="+id_faq+" returning id_faq;");

				this.rs.next();
				result = this.rs.getInt("id_faq");
				
			}catch (Exception e) {
				System.out.print(e.toString());
				return result;
			}
			return result;
	   }
	   
	
	
}
